package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSkuNoticeService;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaysSession() {
//        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<SeckillSessionEntity>()
//                .between("start_time", satrtTime(), endTime());
//        List<SeckillSessionEntity> list = this.baseMapper.selectList(queryWrapper);
//        if (list != null || list.size() > 0){
//            List<SeckillSessionEntity> collect = list.stream().map(session -> {
//                Long id = session.getId();
//                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService
//                        .list(new LambdaQueryWrapper<SeckillSkuRelationEntity>()
//                                .eq(SeckillSkuRelationEntity::getPromotionSessionId, id));
//                session.setRelationSkus(relationEntities);
//                return session;
//            }).collect(Collectors.toList());
//
//            return collect;
//        }
//        return null;
        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", satrtTime(), endTime());

        List<SeckillSessionEntity> list = this.baseMapper.selectList(queryWrapper);
        if (list != null && !list.isEmpty()) {
            List<Long> sessionIds = list.stream().map(SeckillSessionEntity::getId).collect(Collectors.toList());

            List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService
                    .list(new LambdaQueryWrapper<SeckillSkuRelationEntity>()
                            .in(SeckillSkuRelationEntity::getPromotionSessionId, sessionIds));

            Map<Long, List<SeckillSkuRelationEntity>> relationMap = relationEntities.stream()
                    .collect(Collectors.groupingBy(SeckillSkuRelationEntity::getPromotionSessionId));

            list.forEach(session -> session.setRelationSkus(relationMap.get(session.getId())));

            return list;
        }
        return null;
    }

    private String satrtTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}